package com.neu.questionnaireplus.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

  /**
  * @Description: 全局异常处理
  * @Param: [req, e]
  * @return: java.util.Map<java.lang.String,java.lang.Object>
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/30
  */
  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  private Map<String, Object> exceptionHandler(HttpServletRequest req, Exception e) {
    e.printStackTrace();
    log.error("exception handler: ", e);
    Map<String, Object> map = new HashMap<>();
    map.put("code", 1);
    map.put("msg", e.getMessage());
    return map;
  }

  /**
  * @Description: token过期处理
  * @Param: [req, e]
  * @return: java.util.Map<java.lang.String,java.lang.Object>
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/30
  */
  @ExceptionHandler(value = IllegalAccessException.class)
  @ResponseBody
  private Map<String, Object> tokenExpired(HttpServletRequest req, Exception e) {
    e.printStackTrace();
    log.warn("token exception: ", e);
    Map<String, Object> map = new HashMap<>();
    map.put("code", -1);
    map.put("msg", e.getMessage());
    return map;
  }
}

